﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPITest.Filter;


namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ResultFilter]
    public class HomeController : ControllerBase
    {
        private readonly IDataService _dataService;
        private readonly ISingletonService _singletonService;
        private readonly ITransientService _transientService;
        private readonly IScopedService _scopedService;

        public HomeController(IDataService dataService,IScopedService scopedService, ISingletonService singletonService = null, ITransientService transientService = null )
        {
            _dataService = dataService;
            _singletonService = singletonService;
            _transientService = transientService;
            _scopedService = scopedService;
        }
        [HttpGet]
        public void Index([FromServices] IDataService dataService2, [FromServices] IScopedService scopedService2, [FromServices] ITransientService transientService)
        {
            //1：需要自己去实例化类 一旦类发生修改 就会影响很多的地方
            //DataService dataService = new DataService();
            //dataService.GetData();

            //2：依赖注入 吧new实例化 这一步交给IOC容器去完成
            _dataService.GetData();
            dataService2.GetHashCode();
            //这是从构造方法取得
            var s = _scopedService.GetHashCode();
            //这是从方法取得;
            var s1 = scopedService2.GetHashCode();
        }

        //public void Test()
        //{
        //    ClassA a = null;
        //    a.GetHashCode();
        //}
        [HttpGet]
        public string SendMessage(string message,int yodId)
        {
            return "请求成功";
        }
        //客户端响应缓存
        [ResponseCache(Duration =20)]
        [HttpGet]
        public DateTime Now()
        {
            return DateTime.Now;
        }
    }
}
